#include "ClientPlatform.h"
#include "Platform.h"

#include "Log.h"
#include "ClientMsg.h"
#include "ClientMgr.h"

#include "StringUtil.h"
#include "TimerMgr.h"

CClientPlatform::CClientPlatform()
{
}


CClientPlatform::~CClientPlatform()
{
}

bool CClientPlatform::Init()
{

    //lostwang
    CPlatform::Instance().Init();
    //end lostwang 


    if (!CClientMgr::Instance().Init())
    {
        LOG_ERR( "client init fail" );
        return false;
    }

    if (!CTimerMgr::Instance().Init())
    {
        LOG_ERR("Timemgr init fail");
        return false;
    }

    if (!CClientMsg::Instance().Init())
    {
        LOG_ERR("ClientMsg init fail");
        return false;
    }
    return true;
}

int CClientPlatform::Run(int argc, char** argv)
{
    string stContentFromConsole = "";
    vector<string> vecContentSpilt;

    string stCmd = "";

    int iRet = 0;

    CClientMgr &oClientMgr = CClientMgr::Instance();


    oClientMgr.ShowWelcomeToConsole();
    while (1)
    {
        if ( oClientMgr.CheckInputFromConsole())
        {
            stContentFromConsole = oClientMgr.GetInputFromConsole();

            oClientMgr.ClearInputFromConsole();

            vecContentSpilt.clear();
            if (CStringUtil::Spilt(stContentFromConsole, vecContentSpilt) > 0)
            {
                stCmd = vecContentSpilt.front();
                vecContentSpilt.erase(vecContentSpilt.begin());
                iRet = CClientMgr::Instance().Handle(stCmd, vecContentSpilt);

                if (iRet != 0)
                {
                    LOG_ERR("cmd handle fail. cmd:%s, ret:%d", stCmd.c_str(), iRet);
                }
            }

            oClientMgr.ShowWelcomeToConsole();
        }

        //get operate

        //do operate

        //get data

        //deal data

        //lostwang
        CPlatform::Instance().Run(0, NULL);
        //end lostwang 

        CClientMsg::Instance().Polling(1);

        CTimerMgr::Instance().Proc();

    }
}

int CClientPlatform::HandleConsole(vector<string> vecParam)
{
    if (vecParam.size() == 0)
    {
        return 0;
    }
    return 0;
}

int CClientPlatform::HandleMsg(const comm::Head &pbHead, const std::string &pszBody)
{
    return 0;
}
